<template>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" fullscreen>
  <el-card>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章类型" prop="type">
        <el-select 
          v-model="queryParams.type" 
          placeholder="请选择文章类型"
          style="width: 240px;"
        >
          <el-option 
            v-for="item in articleTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
          style="width: 240px;"
        >
          <el-option label="请选择" value="" />
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['wmyl:article:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <!-- <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wmyl:article:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 列表 -->
  <el-card style="margin-top: 10px;">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border>
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="二级标题" align="center" prop="subTitle" />
      <el-table-column label="描述" align="center" prop="mainContent" />
      <el-table-column label="预览图" align="center" prop="mainPhotoPath">
        <template #default="scope">
          <div v-if="scope.row.mainPhotoPath" class="image-preview-container">
            <el-image
              v-for="(img, index) in scope.row.mainPhotoPath"
              :key="index"
              :src="img"
              :preview-src-list="scope.row.mainPhotoPath"
              preview-teleported
              :initial-index="index"
              style="width: 60px; height: 60px; margin-right: 10px; cursor: pointer;"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="相关图片" align="center" prop="photoPath">
        <template #default="scope">
          <div v-if="scope.row.photoPath" class="image-preview-container">
            <el-image
              v-for="(img, index) in scope.row.photoPath"
              :key="index"
              :src="img"
              :preview-src-list="scope.row.photoPath"
              preview-teleported
              :initial-index="index"
              style="width: 60px; height: 60px; margin-right: 10px; cursor: pointer;"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="手机端图片" align="center" prop="mphotoPath">
        <template #default="scope">
          <div v-if="scope.row.mphotoPath" class="image-preview-container">
            <el-image
              v-for="(img, index) in scope.row.mphotoPath"
              :key="index"
              :src="img"
              :preview-src-list="scope.row.mphotoPath"
              preview-teleported
              :initial-index="index"
              style="width: 60px; height: 60px; margin-right: 10px; cursor: pointer;"
              fit="cover"
            />
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '开启' : '删除' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatterHMS"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row)"
            v-hasPermi="['wmyl:article:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['wmyl:article:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </el-card>

  <!-- 详情弹窗 -->
  <ArticleFormDetail ref="formRef" @success="getList" />
  </el-dialog>
</template>

<script setup>
import { dateFormatterHMS } from '@/utils/formatTime'
import download from '@/utils/download'
import { ArticleApi } from '@/api/article'
import ArticleFormDetail from './ArticleFormDetail.vue'
import { useRoute } from 'vue-router'
import { watch } from 'vue'

/** 所有文章 列表 */
defineOptions({ name: 'Article' })

const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数


// 文章类型数组
const articleTypes = ref([
  { label: '产品亮点', value: 1 },
  { label: '应用安全', value: 2 },
]);

// 状态选项
const statusOptions = [
  { label: '开启', value: '1' },
  { label: '删除', value: '2' }
]

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: undefined,
  subTitle: undefined,
  content: undefined,
  type: 1,
  photoPath: undefined,
  mPhotoPath: undefined,
  sort: undefined,
  status: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中


/** 打开弹窗 */
const open = async (type, rowData = null) => {
  dialogVisible.value = true
  dialogTitle.value = '文章管理'
  queryParams.articleId = rowData.id

  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ArticleApi.getArticleItemPage(queryParams)
    list.value = data
    // total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const route = useRoute()

watch(
  () => route.meta.type,
  (newType) => {
    console.log(newType, '--')
    if (newType) {
      // queryParams.type = newType.toString()
      // handleQuery()
    }
  },
  { immediate: true }
)

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type, row) => {
  const data = {
    ...row,
    articleId: queryParams.articleId,
    articleType: queryParams.type,
  }
  formRef.value.open(type, data)
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ArticleApi.deleteArticleItem(id)
    message.success('删除成功')
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ArticleApi.exportArticle(queryParams)
    download.excel(data, '所有文章.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  // 路由监听已自动触发查询
})
</script>

<style scoped>
.image-preview-container {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 0;
}

:deep(.el-image) {
  border-radius: 4px;
  transition: transform 0.2s;
}
</style>

<style>
/* 全局样式，确保图片预览弹窗在最上层 */
.el-image-viewer {
  z-index: 9999 !important;
}
</style>